package com.cchip.cloud.db;

import java.sql.Date;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

import com.cchip.cloud.system.Account;
import com.cchip.cloud.system.Favorite;
import com.cchip.cloud.system.Meeting;

public class MeetingDb {
	private static final String DATABASE_NAME = "meetingdb";
	private static final int DATABASE_VERSION = 1;

	private static MeetingDb instance = null;
	private SQLiteDatabase mDb;
	private DatabaseHelper mHelper;

	private class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(LogColumns.DATABASE_CREATE_TABLE_SQL);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
			db.execSQL(LogColumns.DATABASE_DROP_SQL);
		}

	}

	public static MeetingDb getInstance(Context c) {
		if (instance == null)
			instance = new MeetingDb(c);

		return instance;
	}

	private MeetingDb(Context c) {
		mHelper = new DatabaseHelper(c);

		try {
			mDb = mHelper.getWritableDatabase();
		} catch (SQLiteException e) {
			mDb = mHelper.getReadableDatabase();
		}
	}

	public void addLog(Meeting meeting) {
		ContentValues values = new ContentValues();
		values.put(LogColumns.ID, meeting.roomId);
		values.put(LogColumns.NAME, meeting.roomName);
		Date date = new Date(meeting.startTime);
		Time time = new Time(meeting.startTime);
		values.put(LogColumns.DATE, date.toString());
		values.put(LogColumns.TIME, time.toString());
		values.put(LogColumns.STAMP, meeting.startTime);

		mDb.insert(LogColumns.DATABASE_TABLE_NAME, null, values);
	}

	public Cursor getAllLog() {
		Cursor cursor = mDb.query(LogColumns.DATABASE_TABLE_NAME, null, null,
				null, null, null, LogColumns.STAMP + " DESC");
		return cursor;
	}

	public List<Favorite> getFavorite(int count) {
		HashMap<String, Meeting> map = new HashMap<String, Meeting>();
		Cursor c = mDb.query(LogColumns.DATABASE_TABLE_NAME, null, null, null,
				null, null, null);
		if (c != null) {
			c.moveToFirst();
			while (!c.isAfterLast()) {
				String id = c.getString(LogColumns.INDEX_ID);
				if (map.containsKey(id)) {
					map.get(id).count++;
				} else {
					Meeting meeting = new Meeting();
					meeting.roomId = id;
					meeting.roomName = c.getString(LogColumns.INDEX_NAME);
					meeting.count = 1;
					map.put(id, meeting);
				}
				c.moveToNext();
			}
			c.close();
		}

		ArrayList<Meeting> list = new ArrayList<Meeting>();
		list.addAll(map.values());
		Collections.sort(list);

		int n = (count < list.size()) ? count : list.size();

		ArrayList<Favorite> result = new ArrayList<Favorite>();
		Favorite self = new Favorite();
		self.put(Favorite.KEY_ROOM_ID, Account.my.roomId);
		self.put(Favorite.KEY_ROOM_NAME, Account.my.name);
		result.add(self);
		for (int i = 0; i < n; i++) {
			Favorite fav = new Favorite();
			Meeting meeting = list.get(i);
			if (!meeting.roomId.equals(Account.my.roomId)) {
				fav.put(Favorite.KEY_ROOM_ID, meeting.roomId);
				fav.put(Favorite.KEY_ROOM_NAME, meeting.roomName);

				result.add(fav);
			}
		}

		return result;
	}

}
